import 'dart:async';
import 'package:flutter/material.dart' hide RefreshIndicatorState, RefreshIndicator;
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/foundation.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter/cupertino.dart';

class CcustomCommentLoadFooter extends LoadIndicator {
  final String? idleText, loadingText, noDataText, failedText, canLoadingText;

  /// a builder for re wrap child,If you need to change the boxExtent or background,padding etc.you need outerBuilder to reWrap child
  /// example:
  /// ```dart
  /// outerBuilder:(child){
  ///    return Container(
  ///       color:Colors.red,
  ///       child:child
  ///    );
  /// }
  /// ````
  /// In this example,it will help to add backgroundColor in indicator
  final OuterBuilder? outerBuilder;

  final Widget? idleIcon, loadingIcon, noMoreIcon, failedIcon, canLoadingIcon;

  /// icon and text middle margin
  final double spacing;

  final IconPosition iconPos;

  final TextStyle textStyle;

  final String idString;

  /// notice that ,this attrs only works for LoadStyle.ShowWhenLoading
  final Duration completeDuration;

  const CcustomCommentLoadFooter(this.idString, {
    Key? key,
    VoidCallback? onClick,
    LoadStyle loadStyle = LoadStyle.ShowAlways,
    double height = 80.0,
    this.outerBuilder,
    this.textStyle = const TextStyle(color: Colors.grey),
    this.loadingText,
    this.noDataText,
    this.noMoreIcon,
    this.idleText,
    this.failedText,
    this.canLoadingText,
    this.failedIcon = const Icon(Icons.error, color: Colors.grey),
    this.iconPos = IconPosition.left,
    this.spacing = 15.0,
    this.completeDuration = const Duration(milliseconds: 1000),
    this.loadingIcon,
    this.canLoadingIcon = const Icon(Icons.autorenew, color: Colors.grey),
    this.idleIcon = const Icon(Icons.arrow_upward, color: Colors.grey),
  }) : super(
          key: key,
          loadStyle: loadStyle,
          height: height,
          onClick: onClick,
        );

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState

    return _CcustomLoadFooterState();
  }
}

class _CcustomLoadFooterState extends LoadIndicatorState<CcustomCommentLoadFooter> {
  bool showAsNoMore = false;
  @override
  initState(){
    super.initState();
  }
  Widget _buildText(LoadStatus mode) {
    RefreshString strings = RefreshLocalizations.of(context)?.currentLocalization ?? EnRefreshString();
    return Text(
        mode == LoadStatus.loading
            ? showAsNoMore ? ((widget.noDataText ?? strings.noMoreText) ?? "") : ''
            : LoadStatus.noMore == mode
                ? ((widget.noDataText ?? strings.noMoreText) ?? "")
                : LoadStatus.failed == mode
                    ? ((widget.failedText ?? strings.loadFailedText) ?? "")
                    : LoadStatus.canLoading == mode
                        ? ''
                        : showAsNoMore ? (widget.noDataText ?? "") : (((widget.idleText ?? strings.idleLoadingText) ?? "")),
        style: widget.textStyle);
  }

  Widget _buildIcon(LoadStatus mode) {
    Widget icon = mode == LoadStatus.loading
        ? widget.loadingIcon ??
        (showAsNoMore ? Container() :
            const SizedBox(
              width: 25.0,
              height: 25.0,
              child:
                  /* defaultTargetPlatform == TargetPlatform.iOS
                  ? const CupertinoActivityIndicator()
                  :*/
                  CircularProgressIndicator(
                strokeWidth: 2.0,
                valueColor: AlwaysStoppedAnimation<Color>(Color(0xffe77c10)),
              ),
            ))
        : mode == LoadStatus.noMore
            ? /*widget.noMoreIcon */ Container()
            : mode == LoadStatus.failed
                ? /*widget.failedIcon*/ Container()
                : mode == LoadStatus.canLoading
                    ? /* widget.canLoadingIcon*/ Container()
                    : /*widget.idleIcon */ Container(color: Colors.red,width: 100,height: 10,);
    return icon ?? Container();
  }

  @override
  Future endLoading() {
    // TODO: implement endLoading
    return Future.delayed(widget.completeDuration);
  }

  @override
  Widget buildContent(BuildContext context, LoadStatus mode) {
    // TODO: implement buildChild
    Widget textWidget = _buildText(mode);
    Widget iconWidget = _buildIcon(mode);
    List<Widget> children = <Widget>[iconWidget, textWidget];
    final Widget container = Wrap(
      spacing: widget.spacing,
      textDirection: widget.iconPos == IconPosition.left ? TextDirection.ltr : TextDirection.rtl,
      direction:
          widget.iconPos == IconPosition.bottom || widget.iconPos == IconPosition.top ? Axis.vertical : Axis.horizontal,
      crossAxisAlignment: WrapCrossAlignment.center,
      verticalDirection: widget.iconPos == IconPosition.bottom ? VerticalDirection.up : VerticalDirection.down,
      alignment: WrapAlignment.center,
      children: children,
    );
    return widget.outerBuilder != null
        ? widget.outerBuilder!(container)
        : Container(
            width: 414,
            height: widget.height,
            alignment: Alignment.center,
            child: container,
          );
  }
}
